//
//  ListDetail22ViewController.m
//  JZMCFM
//
//  Created by lanou3g on 15/11/4.
//  Copyright (c) 2015年 陈卓. All rights reserved.
//

#import "ListDetail22ViewController.h"
#import "ListDetail21ViewCell.h"
#import "ListDetailModel.h"

@interface ListDetail22ViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (strong, nonatomic) IBOutlet UITableView *mainTableView;
@property (nonatomic,strong) NSArray *listArray;

@end

@implementation ListDetail22ViewController

#pragma mark 懒加载
-(NSArray*)listArray{
    if (!_listArray) {
        _listArray = [NSArray array];
    }
    return _listArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    self.mainTableView.delegate = self;
    self.mainTableView.dataSource = self;
    
    [self.mainTableView registerNib:[UINib nibWithNibName:@"ListDetail21ViewCell" bundle:nil] forCellReuseIdentifier:@"deatil21"];
    
    [self downLoadDataFromNetWork];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.listArray.count;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    ListDetail21ViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"deatil21" forIndexPath:indexPath];
    ListDetailModel *model = self.listArray[indexPath.row];
    cell.title.text = model.title;
    cell.upDataTime.text = [NSString stringWithFormat:@"%ld",model.createdAt];
    //时间
    cell.timeLabel.text = [NSString stringWithFormat:@"%ld",model.duration];
    cell.comment.text = [NSString stringWithFormat:@"%ld",model.playtimes];
    cell.concern.text = [NSString stringWithFormat:@"%ld",model.comments];
    return cell;
}

#pragma mark 返回cell的高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 60;
}

#pragma mark 设置cell的头部
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 120;
}

-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    UIImageView *view = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 120)];
    
    view = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"listsucai1"]];
    
    if (self.listArray.count != 0) {
        
        UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(147, 10, 80,80)];
        [imageView sd_setImageWithURL:[NSURL URLWithString:self.picUrl]];
        imageView.layer.cornerRadius = 40;
        imageView.clipsToBounds = YES;
        [view addSubview:imageView];
        
        UILabel *lable1 = [[UILabel alloc]initWithFrame:CGRectMake(137, CGRectGetMaxY(imageView.frame) + 5, 60, 20)];
        lable1.text = self.nameTitle;
        lable1.font = [UIFont systemFontOfSize:10];
        [view addSubview:lable1];
            
        }


    return view;
}


#pragma mark 解析数据
-(void)downLoadDataFromNetWork{
    
    NSString *keyID = self.receive;
    NSString *url = @"http://mobile.ximalaya.com/mobile/others/ca/track/";
    NSString *url2 = @"/1/30?device=android";
    //拼接网址
    url = [url stringByAppendingString:keyID];
    url = [url stringByAppendingString:url2];
    NSLog(@"%@",url);
    NSURL *urlString = [NSURL URLWithString:url];
    NSURLRequest *request = [NSURLRequest requestWithURL:urlString];
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        
        NSDictionary *result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSArray *listArray = result[@"list"];
        NSMutableArray *mutArray = [NSMutableArray array];
        for (NSDictionary *dict in listArray) {
            ListDetailModel *model = [[ListDetailModel alloc]init];
            [model setValuesForKeysWithDictionary:dict];
            [mutArray addObject:model];
        }
        self.listArray = [mutArray copy];
        
        
        
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.mainTableView reloadData];
        });
    }];

}




@end
